package com.wk.warehouse.controller;

import java.util.List;
import com.wk.warehouse.entity.Result;
import com.wk.warehouse.entity.Store;
import com.wk.warehouse.service.StoreService;
import com.wk.warehouse.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/store")
public class StoreController {

	@Autowired
	private StoreService storeService;
	
	/**
	 * 分页模糊查询所有仓库
	 */
	@GetMapping("/store-page-list")
	public Result storePageList(Store store, Page page) {
		// 仓库数量
		int totalNum = storeService.storeCount(store);
		page.setTotalNum(totalNum);

		// 模糊查询结果
		List<Store> storeList = storeService.selectStore(store, page);
		page.setResultList(storeList);
		return Result.ok(page);
	}

	/**
	 * 导出数据
	 */
	@GetMapping("/exportTable")
	public Result exportTable(Store store, Page page){
		page = new Page(9999, 1); // 导出模糊查询所有数据
		// 要导出的数据
		List<Store> storeList = storeService.selectStore(store, page);
		return Result.ok(storeList);
	}

	/**
	 * 仓库编号进行唯一性校验
	 */
	@GetMapping("/store-num-check")
	public Result storeNumCheck(String storeNum) {
		int num = storeService.findStoreByNum(storeNum);
		return Result.ok(num==0);
	}

	/**
	 * 添加仓库（ 并对仓库编号进行唯一性校验）
	 */
	@PostMapping("/store-add")
	public Result addStore(@RequestBody Store store) {
		int num = storeService.findStoreByNum(store.getStoreNum());
		if(num>0) return Result.err(Result.CODE_ERR_BUSINESS, "该仓库编码已存在！");
		storeService.addStore(store);
		return Result.ok("添加仓库成功！");
	}
	
	/**
	 * 修改仓库信息
	 */
	@PutMapping("/store-update")
	public Result updateStore(@RequestBody Store store) {
		storeService.updateStore(store);
		return Result.ok("修改仓库成功！");
	}

	/**
	 * 删除仓库信息
	 */
	@DeleteMapping("/store-delete/{storeId}")
	public Result deleteStore(@PathVariable Integer storeId) {
		storeService.deleteStore(storeId);
		return Result.ok("删除仓库成功！");
	}
	
}
